window.onload = function() {
    var btn = document.querySelector('.login-btn');
        // console.log(btn)
        btn.onclick = function() {
        // 获取数据
        var username = document.querySelector('.username').value;
        // console.log(username)
        var password = document.querySelector('.password').value;
        // console.log(password)
        var repass = document.querySelector('.repass').value;
        // console.log(repass)
        var nickname = document.querySelector('.nickname').value;
        // console.log(nickname)
        // 做表单验证
        var reg = /(^[az0-9]\w{4,11}$)|(^[1-9]\d{4,10}@qq\.com$)|(^[a-zA-Z]\w{5,17}@((126|163)\.com)|(yeah\.net)$)|(^1[3-9]\d{9}$)/
        if(!reg.test(username)) {
            alert('用户名不规范')
            return false
        }
        var reg = /^.{6,12}$/
        if(!reg.test(password)) {
            alert('密码不符合规则')
            return false
        }

        if(repass != password) {
            alert('两次密码不一致')
            return false
        }
        var reg = /^[a-z0-9]\w{4,11}$/
        if(!reg.test(nickname)) {
            alert('昵称不规范')
            return false
        }

        console.log(111);

        // 发送ajax请求
        ajax({
            url: '/api/register',
            method: 'post',
            data: {
                username,
                password,
                nickname
            },
            fn: function(res) {
                res = JSON.parse(res)
                if(res.code == 1) {
                    // 使用弹出层
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 2000
                    }, function() {
                        location.href = 'login.html'
                    })
                    // alert(res.msg)
                    // location.href = "login.html"
                }else {
                    layer.msg(res.msg, {
                        icon: 2,
                        time: 2000
                    })
                }
            }
        })
    }
}